package com.zn.web.rpc;

import com.zn.entity.ContractAnalysis;
import com.zn.util.HttpClientUtil;
import com.zn.vo.Response;
import com.zn.vo.SinaMarketDataField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author YinJiaWei
 * @version 2018/8/25 20:12
 */
@Component
public class MdRemoteCall {
    private static final Logger LOGGER = LoggerFactory.getLogger(MdRemoteCall.class);
    @Value("${jiejiari.url}")
    private String url;
    @Value("${jiejiari.interface.checkholiday}")
    private String checkHoliday;
    @Value("${sinajs.ip}")
    private String sinajsIp;
    @Value("${sinajsetf.ip}")
    private String sinajsetfIp;
    @Value("${sinajs.option.url}")
    private String sinajsOptionUrl;
    @Value("${sinajs.option.min.line}")
    private String sinajsOptionMinLine;
    @Value("${sinajs.option.fiveDay.line}")
    private String sinajsOptionFiveDayLine;
    @Value("${sinajs.option.day.line}")
    private String sinajsOptionDayLine;
    @Value("${eastmoney.month.url}")
    private String eastmoneyMonthUrl;
    @Value("${totality.task.url}")
    private String totalityTaskUrl;

    public Response checkHoliday(String date){
        Response response = HttpClientUtil.post(url,
                HttpClientUtil.Builder.builder(checkHoliday)
                        .with("date", date)
        );
        return response;
    }

    public String getSinaOptionMinLine(String targetId){
        String sinaTick=HttpClientUtil.get(HttpClientUtil.Builder.builder("?symbol=CON_OP_" + targetId), sinajsIp + sinajsOptionUrl + sinajsOptionMinLine);
        return sinaTick;
    }

    public String getSinaOptionDayLine(String targetId){
        String sinaTick=HttpClientUtil.get(HttpClientUtil.Builder.builder("?symbol=CON_OP_" + targetId), sinajsIp + sinajsOptionUrl + sinajsOptionDayLine);
        return sinaTick;
    }

    public String getSinaOptionFiveDayLine(String targetId){
        String sinaTick=HttpClientUtil.get(HttpClientUtil.Builder.builder("?symbol=CON_OP_" + targetId), sinajsIp + sinajsOptionUrl + sinajsOptionFiveDayLine);
        return sinaTick;
    }

    public String getEastmoneyMonthLine(String targetId){
        String sinaTick=eastmoneyMonthUrl+"&id="+targetId+"_SO";
        return sinaTick;
    }

    public String getEtfMdInfo(String targetCode){
        //https://stock.finance.sina.com.cn/list=s_sh510050
        String etfMdStr=HttpClientUtil.get(HttpClientUtil.Builder.builder("/list=s_sh"+targetCode), sinajsetfIp);
        return etfMdStr;
    }

    /**
     * 获取参考行情的方法
     * @param referCode
     * @return
     */
    public String getReferMdInfo(String referCode){
        String referMdStr=HttpClientUtil.get(HttpClientUtil.Builder.builder("/list="+referCode), sinajsetfIp);
        return referMdStr;
    }

    public String [] subStringSinaData(String context){
        String [] mdInfoArray=context.split("=");
        if(mdInfoArray.length<2){
            return null;
        }
        String mdInfoElement=mdInfoArray[mdInfoArray.length-1];
        String mdInfoStr=mdInfoElement.substring(1,mdInfoElement.length()-2);
        String [] mdInfos=mdInfoStr.split(",");
        return mdInfos;
    }

    public void sendSocketStatus(String businessUnique,Integer listenerType,Integer socketStatus,String socketIp,Integer socketPort){
        try{
            HttpClientUtil.post(totalityTaskUrl,
                    HttpClientUtil.Builder.builder("/management/readSocketStatus")
                            .with("businessUnique", businessUnique)
                            .with("listenerType", listenerType)
                            .with("socketStatus", socketStatus)
                            .with("socketIp", socketIp)
                            .with("socketPort", socketPort)
            );
        }catch (Exception e){
            LOGGER.error("sendSocketStatus......"+e);
        }
    }

    /**
     * 获取合约分析数据
     * @param code 合约code
     * @return
     */
    public ContractAnalysis getContractAnalysis(String code){
        try {
            String contractAnalysisStr=HttpClientUtil.get(HttpClientUtil.Builder.builder("/list=CON_SO_" + code), sinajsetfIp);
            String [] contractAnalysisArray=subStringSinaData(contractAnalysisStr);
            String contractName=contractAnalysisArray[0],volume=contractAnalysisArray[4],delta=contractAnalysisArray[5],gamma=contractAnalysisArray[6],theta=contractAnalysisArray[7]
                    ,vega=contractAnalysisArray[8],wavePrice=contractAnalysisArray[9],highPrice=contractAnalysisArray[10],lowPrice=contractAnalysisArray[11]
                    ,tradeCode=contractAnalysisArray[12],execsPrice=contractAnalysisArray[13],lastPrice=contractAnalysisArray[14],theoryPrice=contractAnalysisArray[15],priceStatus=contractAnalysisArray[16];
            ContractAnalysis contractAnalysis=new ContractAnalysis(contractName,volume,delta,gamma,theta,vega,wavePrice,highPrice,lowPrice,tradeCode,execsPrice,lastPrice,theoryPrice,priceStatus);
            return contractAnalysis;
        }catch (Exception e){
            LOGGER.error("getContractAnalysis......"+e);
            return null;
        }
    }

    public SinaMarketDataField getSinaMdInfo(String instrumentCode){
        String sinaTick=HttpClientUtil.get(HttpClientUtil.Builder.builder("/list=CON_OP_"+instrumentCode), sinajsetfIp);
        String [] sinaMdInfos=subStringSinaData(sinaTick);
        SinaMarketDataField sinaMarketDataField=null;
        if(sinaMdInfos!=null) {
            sinaMarketDataField = new SinaMarketDataField(
                    sinaMdInfos[0], sinaMdInfos[1], sinaMdInfos[2], sinaMdInfos[3],
                    sinaMdInfos[4], sinaMdInfos[5], sinaMdInfos[6], sinaMdInfos[7],
                    sinaMdInfos[8], sinaMdInfos[9], sinaMdInfos[10], sinaMdInfos[11],
                    sinaMdInfos[12], sinaMdInfos[13], sinaMdInfos[14], sinaMdInfos[15],
                    sinaMdInfos[16], sinaMdInfos[17], sinaMdInfos[18], sinaMdInfos[19],
                    sinaMdInfos[20], sinaMdInfos[21], sinaMdInfos[22], sinaMdInfos[23],
                    sinaMdInfos[24], sinaMdInfos[25], sinaMdInfos[26], sinaMdInfos[27],
                    sinaMdInfos[28], sinaMdInfos[29], sinaMdInfos[30], sinaMdInfos[31],
                    sinaMdInfos[32], sinaMdInfos[37], sinaMdInfos[37], sinaMdInfos[39],
                    sinaMdInfos[40], sinaMdInfos[41], sinaMdInfos[42]
            );
        }
        return sinaMarketDataField;
    }
}
